<template>
  <div class="update-gender">
    <van-picker
      title="性别"
      show-toolbar
      :columns="columns"
      @cancel="$emit('close')"
      @change="onChange"
      :default-index="localGender"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { updateUserProfile } from '@/apis/user'
export default {
  name: 'UpdateGender',
  props: {
    gender: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      localGender: this.gender,
      columns: ['男', '女']
    }
  },
  methods: {
    onChange (picker, value, index) {
      this.localGender = index
    },
    async onConfirm () {
      this.$toast.loading({
        message: '保存中',
        forbidClick: true
      })
      await updateUserProfile({
        gender: this.localGender
      })
      this.$toast.success('保存成功')
      this.$emit('update:gender', this.localGender)
      this.$emit('close')
    }
  }
}
</script>

<style lang="less" scoped>
.field-wrap{
  padding: 10px;
}
</style>
